﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section styles{
    <style>
        .layui-layer-btn .layui-layer-btn1 {
            background-color: #f56c6c;
            color:#fff;
        }
    </style>
}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form form-search" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
                </div>
                 <label class="layui-form-label">类型</label>
                <div class="layui-input-inline">
                    <select name="notifytype">
                        <option value="">全部</option>
                        <option value="1">公告</option>
                        <option value="0">通知</option>
                    </select>
                </div>
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="status">
                        <option value="">全部</option>
                        <option value="1">发布</option>
                        <option value="0">草稿</option>
                    </select>
                </div>
                <label class="layui-form-label">日期范围</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="createtime" name="createtime" placeholder="请选择开始日期范围" >
                </div>
                
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="notice-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="notice-table" lay-filter="notice-table"></table>
    </div>
</div>


<script type="text/html" id="notice-toolbar">

    @{await Html.RenderPartialAsync("_ToolbarAuthTpl", new SystemFunctionModel
        {
            MenuKey =SysMenuConstant.MENU_NOTICE_MANAGE,
            FunDictionary = new Dictionary<string, string> {  { "Add", "新增" },  { "Delete", "删除" } }
        });
    }
</script>

<script type="text/html" id="notice-bar">
    @{
      await  Html.RenderPartialAsync("_OperationTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_NOTICE_MANAGE,
            FunDictionary = new Dictionary<string, string> {  { "Update", "编辑" }, { "Delete", "删除" } }
        });
    }
</script>

<script type="text/html" id="notice-noticType">
    {{#if (d.noticType == 1) { }}
       <span class="layui-badge-rim">公告</span>
    {{# }else { }}
       <span class="layui-badge-rim">通知</span>
    {{# } }}
    
</script>

<script type="text/html" id="notice-status">
  {{#if (d.status == 1) { }}
      <span class="layui-badge layui-bg-green">已发布</span>
    {{# }else { }}
      <span class="layui-badge">未发布</span>
    {{# } }}
</script>
<script type="text/html" id="notice-name">
    <a class="cell-a-color" lay-event="detail" href="javascript:void(0)">{{d.title}}</a>
</script>
@section scripts{

       <script>
        layui.use(['util','table', 'form', 'lscom','laydate'], function () {
            let util = layui.util;
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let lscom = layui.lscom;
            let laydate = layui.laydate;
            let MODULE_PATH = "/Systems/Notice/";

            var istoolbar = $.trim($("#notice-bar").html())=="" ? true : false;
            let cols = [
                [{
                    type: 'checkbox'
                },
                    {
                        title: '标题',
                        field: 'title',
                        align: 'left',
                        width: 200,
                        templet: "#notice-name"
                    },
                    {
                        title: '类型',
                        field: 'noticType',
                        align: 'left',
                        templet: '#notice-noticType',
                        width: 150
                    },
                    {
                        title: '发布人',
                        field: 'publicUserName',
                        align: 'left',
                        width: 150
                    },
                    {
                        title: '发布时间',
                        field: 'publicTime',
                        align: 'left',
                        width: 150,
                        templet:function(d){
                            return  util.toDateString(d.publicTime, 'yyyy-MM-dd')
                        }
                    },
                    {
                        title: '状态',
                        field: 'status',
                        align: 'center',
                        width: 150,
                        templet: '#notice-status'
                    },
                    {
                        title: '操作',
                        templet: '#notice-bar',
                        align: 'center',
                        width: 180,
                        hide:istoolbar
                    }
                ]
            ];

             //开始日期
            laydate.render({
                elem: '#createtime',
                range: true
            });
            table.render({
                elem: '#notice-table',
                url: MODULE_PATH + "GetList",
                page: true,
                cols: cols,
                skin: 'line',
                height: 'full-148',
                toolbar: '#notice-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(notice-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'Delete') {
                    window.remove(data.id);
                } else if (obj.event === 'Update') {
                    if (data.status==1) {
                        lscom.notify.warn('已发布通知公告不允许修改');
                    }
                    else{
                        window.edit(data.id);
                    }
                } else if (obj.event === 'detail') {
                    window.detail(data.id);
                }
            });

            table.on('toolbar(notice-table)', function (obj) {
                if (obj.event === 'Add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'Delete') {

                    let checkData = table.checkStatus(obj.config.id).data;
                    if (checkData.length === 0) {
                        parent.layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var ids = checkData.map(function (d) { return d.id; });
                    window.remove(ids.join(','));
                }
            });

            form.on('submit(notice-query)', function (data) {
                table.reload('notice-table', {
                    where: data.field
                })
                return false;
            });

            window.add = function () {
                parent.layer.open({
                    type: 2,
                    title: '新增通知公告',
                    shade: 0.1,
                    area: ['80%', '80%'],
                    content: MODULE_PATH + 'CreateNotice',
                    btn: ['确定','取消'],
                    //btn: ['发布','草稿', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm(1);
                    }
                    //,btn1: function(index, layero) {
                    //    parent.window['layui-layer-iframe' + index].submitForm(0);
                    //}
                });
            }
            window.edit = function (id) {
                parent.layer.open({
                    type: 2,
                    title: '修改通知公告',
                    shade: 0.1,
                     area: ['80%', '80%'],
                    content: MODULE_PATH + 'EditNotice' + lscom.utils.formatString("?id={0}", id),
                    btn: ['确定','取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }

            window.remove = function (data) {
                parent.layer.confirm('确定删除吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    parent.layer.close(index);
                    let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'DelNotice',
                        data: JSON.stringify({ "ids": data }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            table.reload('notice-table');
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }
             window.detail = function (id) {
                parent.layer.open({
                    type: 2,
                    title: '通知公告详情',
                    shade: 0.1,
                    area: ['80%', '80%'],
                    content: MODULE_PATH + 'Info' + lscom.utils.formatString("?id={0}", id),
                    btn: ['取消']
                });
            }
            window.saveCallback = function (data) {
                parent.layer.close(data.index);
                lscom.notify.success(data.message);
                table.reload('notice-table');
            }
            window.refresh = function () {
                table.reload('notice-table');
            }
        });
    </script>
}
